<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>autocomplete.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_ac_autocomplete.js.html">autocomplete.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Gmail-like AutoComplete logic.
<a name="line17"></a> *
<a name="line18"></a> * @see ../../demos/autocomplete-basic.html
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.ui.ac.AutoComplete&#39;);
<a name="line22"></a>goog.provide(&#39;goog.ui.ac.AutoComplete.EventType&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.events&#39;);
<a name="line25"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * This is the central manager class for an AutoComplete instance.
<a name="line31"></a> *
<a name="line32"></a> * @param {Object} matcher A data source and row matcher, implements
<a name="line33"></a> *        &lt;code&gt;requestMatchingRows(token, maxMatches, matchCallback)&lt;/code&gt;.
<a name="line34"></a> * @param {goog.events.EventTarget} renderer An object that implements
<a name="line35"></a> *        &lt;code&gt;
<a name="line36"></a> *          isVisible():boolean&lt;br&gt;
<a name="line37"></a> *          renderRows(rows:Array, token:string, target:Element);&lt;br&gt;
<a name="line38"></a> *          hiliteId(row-id:number);&lt;br&gt;
<a name="line39"></a> *          dismiss();&lt;br&gt;
<a name="line40"></a> *          dispose():
<a name="line41"></a> *        &lt;/code&gt;.
<a name="line42"></a> * @param {Object} selectionHandler An object that implements
<a name="line43"></a> *        &lt;code&gt;
<a name="line44"></a> *          selectRow(row);&lt;br&gt;
<a name="line45"></a> *          update(opt_force);
<a name="line46"></a> *        &lt;/code&gt;.
<a name="line47"></a> *
<a name="line48"></a> * @constructor
<a name="line49"></a> * @extends {goog.events.EventTarget}
<a name="line50"></a> */
<a name="line51"></a>goog.ui.ac.AutoComplete = function(matcher, renderer, selectionHandler) {
<a name="line52"></a>  goog.events.EventTarget.call(this);
<a name="line53"></a>
<a name="line54"></a>  /**
<a name="line55"></a>   * A data-source which provides autocomplete suggestions.
<a name="line56"></a>   * @type {Object}
<a name="line57"></a>   * @protected
<a name="line58"></a>   * @suppress {underscore}
<a name="line59"></a>   */
<a name="line60"></a>  this.matcher_ = matcher;
<a name="line61"></a>
<a name="line62"></a>  /**
<a name="line63"></a>   * A handler which interacts with the input DOM element (textfield, textarea,
<a name="line64"></a>   * or richedit).
<a name="line65"></a>   * @type {Object}
<a name="line66"></a>   * @protected
<a name="line67"></a>   * @suppress {underscore}
<a name="line68"></a>   */
<a name="line69"></a>  this.selectionHandler_ = selectionHandler;
<a name="line70"></a>
<a name="line71"></a>  /**
<a name="line72"></a>   * A renderer to render/show/highlight/hide the autocomplete menu.
<a name="line73"></a>   * @type {goog.events.EventTarget}
<a name="line74"></a>   * @protected
<a name="line75"></a>   * @suppress {underscore}
<a name="line76"></a>   */
<a name="line77"></a>  this.renderer_ = renderer;
<a name="line78"></a>  goog.events.listen(renderer, [
<a name="line79"></a>    goog.ui.ac.AutoComplete.EventType.HILITE,
<a name="line80"></a>    goog.ui.ac.AutoComplete.EventType.SELECT,
<a name="line81"></a>    goog.ui.ac.AutoComplete.EventType.CANCEL_DISMISS,
<a name="line82"></a>    goog.ui.ac.AutoComplete.EventType.DISMISS], this);
<a name="line83"></a>
<a name="line84"></a>  /**
<a name="line85"></a>   * Currently typed token which will be used for completion.
<a name="line86"></a>   * @type {?string}
<a name="line87"></a>   * @protected
<a name="line88"></a>   * @suppress {underscore}
<a name="line89"></a>   */
<a name="line90"></a>  this.token_ = null;
<a name="line91"></a>
<a name="line92"></a>  /**
<a name="line93"></a>   * Autcomplete suggestion items.
<a name="line94"></a>   * @type {Array}
<a name="line95"></a>   * @protected
<a name="line96"></a>   * @suppress {underscore}
<a name="line97"></a>   */
<a name="line98"></a>  this.rows_ = [];
<a name="line99"></a>
<a name="line100"></a>  /**
<a name="line101"></a>   * Id of the currently highlighted row.
<a name="line102"></a>   * @type {number}
<a name="line103"></a>   * @protected
<a name="line104"></a>   * @suppress {underscore}
<a name="line105"></a>   */
<a name="line106"></a>  this.hiliteId_ = -1;
<a name="line107"></a>
<a name="line108"></a>  /**
<a name="line109"></a>   * Id of the first row in autocomplete menu. Note that new ids are assigned
<a name="line110"></a>   * everytime new suggestions are fetched.
<a name="line111"></a>   * @type {number}
<a name="line112"></a>   * @protected
<a name="line113"></a>   * @suppress {underscore}
<a name="line114"></a>   */
<a name="line115"></a>  this.firstRowId_ = 0;
<a name="line116"></a>
<a name="line117"></a>  /**
<a name="line118"></a>   * The target HTML node for displaying.
<a name="line119"></a>   * @type {Element}
<a name="line120"></a>   * @protected
<a name="line121"></a>   * @suppress {underscore}
<a name="line122"></a>   */
<a name="line123"></a>  this.target_ = null;
<a name="line124"></a>
<a name="line125"></a>  /**
<a name="line126"></a>   * The timer id for dismissing autocomplete menu with a delay.
<a name="line127"></a>   * @type {?number}
<a name="line128"></a>   * @private
<a name="line129"></a>   */
<a name="line130"></a>  this.dismissTimer_ = null;
<a name="line131"></a>
<a name="line132"></a>  /**
<a name="line133"></a>   * Mapping from text input element to the anchor element. If the
<a name="line134"></a>   * mapping does not exist, the input element will act as the anchor
<a name="line135"></a>   * element.
<a name="line136"></a>   * @type {Object.&lt;Element&gt;}
<a name="line137"></a>   * @private
<a name="line138"></a>   */
<a name="line139"></a>  this.inputToAnchorMap_ = {};
<a name="line140"></a>};
<a name="line141"></a>goog.inherits(goog.ui.ac.AutoComplete, goog.events.EventTarget);
<a name="line142"></a>
<a name="line143"></a>
<a name="line144"></a>/**
<a name="line145"></a> * The maximum number of matches that should be returned
<a name="line146"></a> * @type {number}
<a name="line147"></a> * @private
<a name="line148"></a> */
<a name="line149"></a>goog.ui.ac.AutoComplete.prototype.maxMatches_ = 10;
<a name="line150"></a>
<a name="line151"></a>
<a name="line152"></a>/**
<a name="line153"></a> * True iff the first row should automatically be highlighted
<a name="line154"></a> * @type {boolean}
<a name="line155"></a> * @private
<a name="line156"></a> */
<a name="line157"></a>goog.ui.ac.AutoComplete.prototype.autoHilite_ = true;
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * True iff the user can unhilight all rows by pressing the up arrow.
<a name="line162"></a> * @type {boolean}
<a name="line163"></a> * @private
<a name="line164"></a> */
<a name="line165"></a>goog.ui.ac.AutoComplete.prototype.allowFreeSelect_ = false;
<a name="line166"></a>
<a name="line167"></a>
<a name="line168"></a>/**
<a name="line169"></a> * True iff item selection should wrap around from last to first. If
<a name="line170"></a> *     allowFreeSelect_ is on in conjunction, there is a step of free selection
<a name="line171"></a> *     before wrapping.
<a name="line172"></a> * @type {boolean}
<a name="line173"></a> * @private
<a name="line174"></a> */
<a name="line175"></a>goog.ui.ac.AutoComplete.prototype.wrap_ = false;
<a name="line176"></a>
<a name="line177"></a>
<a name="line178"></a>/**
<a name="line179"></a> * Whether completion from suggestion triggers fetching new suggestion.
<a name="line180"></a> * @type {boolean}
<a name="line181"></a> * @private
<a name="line182"></a> */
<a name="line183"></a>goog.ui.ac.AutoComplete.prototype.triggerSuggestionsOnUpdate_ = false;
<a name="line184"></a>
<a name="line185"></a>
<a name="line186"></a>/**
<a name="line187"></a> * Events associated with the autocomplete
<a name="line188"></a> * @enum {string}
<a name="line189"></a> */
<a name="line190"></a>goog.ui.ac.AutoComplete.EventType = {
<a name="line191"></a>
<a name="line192"></a>  /** A row has been highlighted by the renderer */
<a name="line193"></a>  ROW_HILITE: &#39;rowhilite&#39;,
<a name="line194"></a>
<a name="line195"></a>  // Note: The events below are used for internal autocomplete events only and
<a name="line196"></a>  // should not be used in non-autocomplete code.
<a name="line197"></a>
<a name="line198"></a>  /** A row has been mouseovered and should be highlighted by the renderer. */
<a name="line199"></a>  HILITE: &#39;hilite&#39;,
<a name="line200"></a>
<a name="line201"></a>  /** A row has been selected by the renderer */
<a name="line202"></a>  SELECT: &#39;select&#39;,
<a name="line203"></a>
<a name="line204"></a>  /** A dismiss event has occurred */
<a name="line205"></a>  DISMISS: &#39;dismiss&#39;,
<a name="line206"></a>
<a name="line207"></a>  /** Event that cancels a dismiss event */
<a name="line208"></a>  CANCEL_DISMISS: &#39;canceldismiss&#39;,
<a name="line209"></a>
<a name="line210"></a>  /**
<a name="line211"></a>   * Field value was updated.  A row field is included and is non-null when a
<a name="line212"></a>   * row has been selected.  The value of the row typically includes fields:
<a name="line213"></a>   * contactData and formattedValue as well as a toString function (though none
<a name="line214"></a>   * of these fields are guaranteed to exist).  The row field may be used to
<a name="line215"></a>   * return custom-type row data.
<a name="line216"></a>   */
<a name="line217"></a>  UPDATE: &#39;update&#39;,
<a name="line218"></a>
<a name="line219"></a>  /**
<a name="line220"></a>   * The list of suggestions has been updated, usually because either the list
<a name="line221"></a>   * has opened, or because the user has typed another character and the
<a name="line222"></a>   * suggestions have been updated, or the user has dismissed the autocomplete.
<a name="line223"></a>   */
<a name="line224"></a>  SUGGESTIONS_UPDATE: &#39;suggestionsupdate&#39;
<a name="line225"></a>};
<a name="line226"></a>
<a name="line227"></a>
<a name="line228"></a>/**
<a name="line229"></a> * Returns the renderer that renders/shows/highlights/hides the autocomplete
<a name="line230"></a> * menu.
<a name="line231"></a> * @return {goog.events.EventTarget} Renderer used by the this widget.
<a name="line232"></a> */
<a name="line233"></a>goog.ui.ac.AutoComplete.prototype.getRenderer = function() {
<a name="line234"></a>  return this.renderer_;
<a name="line235"></a>};
<a name="line236"></a>
<a name="line237"></a>
<a name="line238"></a>/**
<a name="line239"></a> * Generic event handler that handles any events this object is listening to.
<a name="line240"></a> * @param {goog.events.Event} e Event Object.
<a name="line241"></a> */
<a name="line242"></a>goog.ui.ac.AutoComplete.prototype.handleEvent = function(e) {
<a name="line243"></a>  if (e.target == this.renderer_) {
<a name="line244"></a>    switch (e.type) {
<a name="line245"></a>      case goog.ui.ac.AutoComplete.EventType.HILITE:
<a name="line246"></a>        this.hiliteId(/** @type {number} */ (e.row));
<a name="line247"></a>        break;
<a name="line248"></a>
<a name="line249"></a>      case goog.ui.ac.AutoComplete.EventType.SELECT:
<a name="line250"></a>        this.selectHilited();
<a name="line251"></a>        break;
<a name="line252"></a>
<a name="line253"></a>      case goog.ui.ac.AutoComplete.EventType.CANCEL_DISMISS:
<a name="line254"></a>        this.cancelDelayedDismiss();
<a name="line255"></a>        break;
<a name="line256"></a>
<a name="line257"></a>      case goog.ui.ac.AutoComplete.EventType.DISMISS:
<a name="line258"></a>        this.dismissOnDelay();
<a name="line259"></a>        break;
<a name="line260"></a>    }
<a name="line261"></a>  }
<a name="line262"></a>};
<a name="line263"></a>
<a name="line264"></a>
<a name="line265"></a>/**
<a name="line266"></a> * Sets the max number of matches to fetch from the Matcher.
<a name="line267"></a> *
<a name="line268"></a> * @param {number} max Max number of matches.
<a name="line269"></a> */
<a name="line270"></a>goog.ui.ac.AutoComplete.prototype.setMaxMatches = function(max) {
<a name="line271"></a>  this.maxMatches_ = max;
<a name="line272"></a>};
<a name="line273"></a>
<a name="line274"></a>
<a name="line275"></a>/**
<a name="line276"></a> * Sets whether or not the first row should be highlighted by default.
<a name="line277"></a> *
<a name="line278"></a> * @param {boolean} autoHilite true iff the first row should be
<a name="line279"></a> *      highlighted by default.
<a name="line280"></a> */
<a name="line281"></a>goog.ui.ac.AutoComplete.prototype.setAutoHilite = function(autoHilite) {
<a name="line282"></a>  this.autoHilite_ = autoHilite;
<a name="line283"></a>};
<a name="line284"></a>
<a name="line285"></a>
<a name="line286"></a>/**
<a name="line287"></a> * Sets whether or not the up/down arrow can unhilite all rows.
<a name="line288"></a> *
<a name="line289"></a> * @param {boolean} allowFreeSelect true iff the up arrow can unhilite all rows.
<a name="line290"></a> */
<a name="line291"></a>goog.ui.ac.AutoComplete.prototype.setAllowFreeSelect =
<a name="line292"></a>    function(allowFreeSelect) {
<a name="line293"></a>  this.allowFreeSelect_ = allowFreeSelect;
<a name="line294"></a>};
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>/**
<a name="line298"></a> * Sets whether or not selections can wrap around the edges.
<a name="line299"></a> *
<a name="line300"></a> * @param {boolean} wrap true iff sections should wrap around the edges.
<a name="line301"></a> */
<a name="line302"></a>goog.ui.ac.AutoComplete.prototype.setWrap = function(wrap) {
<a name="line303"></a>  this.wrap_ = wrap;
<a name="line304"></a>};
<a name="line305"></a>
<a name="line306"></a>
<a name="line307"></a>/**
<a name="line308"></a> * Sets whether or not to request new suggestions immediately after completion
<a name="line309"></a> * of a suggestion.
<a name="line310"></a> *
<a name="line311"></a> * @param {boolean} triggerSuggestionsOnUpdate true iff completion should fetch
<a name="line312"></a> *     new suggestions.
<a name="line313"></a> */
<a name="line314"></a>goog.ui.ac.AutoComplete.prototype.setTriggerSuggestionsOnUpdate = function(
<a name="line315"></a>    triggerSuggestionsOnUpdate) {
<a name="line316"></a>  this.triggerSuggestionsOnUpdate_ = triggerSuggestionsOnUpdate;
<a name="line317"></a>};
<a name="line318"></a>
<a name="line319"></a>
<a name="line320"></a>/**
<a name="line321"></a> * Sets the token to match against.  This triggers calls to the Matcher to
<a name="line322"></a> * fetch the matches (up to maxMatches), and then it triggers a call to
<a name="line323"></a> * &lt;code&gt;renderer.renderRows()&lt;/code&gt;.
<a name="line324"></a> *
<a name="line325"></a> * @param {string} token The string for which to search in the Matcher.
<a name="line326"></a> * @param {string=} opt_fullString Optionally, the full string in the input
<a name="line327"></a> *     field.
<a name="line328"></a> */
<a name="line329"></a>goog.ui.ac.AutoComplete.prototype.setToken = function(token, opt_fullString) {
<a name="line330"></a>  if (this.token_ == token) {
<a name="line331"></a>    return;
<a name="line332"></a>  }
<a name="line333"></a>  this.token_ = token;
<a name="line334"></a>  this.matcher_.requestMatchingRows(this.token_,
<a name="line335"></a>      this.maxMatches_, goog.bind(this.matchListener_, this), opt_fullString);
<a name="line336"></a>  this.cancelDelayedDismiss();
<a name="line337"></a>};
<a name="line338"></a>
<a name="line339"></a>
<a name="line340"></a>/**
<a name="line341"></a> * Gets the current target HTML node for displaying autocomplete UI.
<a name="line342"></a> * @return {Element} The current target HTML node for displaying autocomplete
<a name="line343"></a> *     UI.
<a name="line344"></a> */
<a name="line345"></a>goog.ui.ac.AutoComplete.prototype.getTarget = function() {
<a name="line346"></a>  return this.target_;
<a name="line347"></a>};
<a name="line348"></a>
<a name="line349"></a>
<a name="line350"></a>/**
<a name="line351"></a> * Sets the current target HTML node for displaying autocomplete UI.
<a name="line352"></a> * Can be an implementation specific definition of how to display UI in relation
<a name="line353"></a> * to the target node.
<a name="line354"></a> * This target will be passed into  &lt;code&gt;renderer.renderRows()&lt;/code&gt;
<a name="line355"></a> *
<a name="line356"></a> * @param {Element} target The current target HTML node for displaying
<a name="line357"></a> *     autocomplete UI.
<a name="line358"></a> */
<a name="line359"></a>goog.ui.ac.AutoComplete.prototype.setTarget = function(target) {
<a name="line360"></a>  this.target_ = target;
<a name="line361"></a>};
<a name="line362"></a>
<a name="line363"></a>
<a name="line364"></a>/**
<a name="line365"></a> * @return {boolean} Whether the autocomplete&#39;s renderer is open.
<a name="line366"></a> */
<a name="line367"></a>goog.ui.ac.AutoComplete.prototype.isOpen = function() {
<a name="line368"></a>  return this.renderer_.isVisible();
<a name="line369"></a>};
<a name="line370"></a>
<a name="line371"></a>
<a name="line372"></a>/**
<a name="line373"></a> * @return {number} Number of rows in the autocomplete.
<a name="line374"></a> */
<a name="line375"></a>goog.ui.ac.AutoComplete.prototype.getRowCount = function() {
<a name="line376"></a>  return this.rows_.length;
<a name="line377"></a>};
<a name="line378"></a>
<a name="line379"></a>
<a name="line380"></a>/**
<a name="line381"></a> * Moves the hilite to the next row, or does nothing if we&#39;re already at the
<a name="line382"></a> * end of the current set of matches.  Calls renderer.hiliteId() when there&#39;s
<a name="line383"></a> * something to do.
<a name="line384"></a> * @return {boolean} Returns true on a successful hilite.
<a name="line385"></a> */
<a name="line386"></a>goog.ui.ac.AutoComplete.prototype.hiliteNext = function() {
<a name="line387"></a>  var lastId = this.firstRowId_ + this.rows_.length - 1;
<a name="line388"></a>  if (this.hiliteId_ &gt;= this.firstRowId_ &amp;&amp; this.hiliteId_ &lt; lastId) {
<a name="line389"></a>    this.hiliteId(this.hiliteId_ + 1);
<a name="line390"></a>    return true;
<a name="line391"></a>  } else if (this.hiliteId_ == -1) {
<a name="line392"></a>    this.hiliteId(this.firstRowId_);
<a name="line393"></a>    return true;
<a name="line394"></a>  } else if (this.hiliteId_ == lastId) {
<a name="line395"></a>    if (this.allowFreeSelect_) {
<a name="line396"></a>      this.hiliteId(-1);
<a name="line397"></a>      return false;
<a name="line398"></a>    } else if (this.wrap_) {
<a name="line399"></a>      this.hiliteId(this.firstRowId_);
<a name="line400"></a>      return true;
<a name="line401"></a>    }
<a name="line402"></a>  }
<a name="line403"></a>  return false;
<a name="line404"></a>};
<a name="line405"></a>
<a name="line406"></a>
<a name="line407"></a>/**
<a name="line408"></a> * Moves the hilite to the previous row, or does nothing if we&#39;re already at
<a name="line409"></a> * the beginning of the current set of matches.  Calls renderer.hiliteId()
<a name="line410"></a> * when there&#39;s something to do.
<a name="line411"></a> * @return {boolean} Returns true on a successful hilite.
<a name="line412"></a> */
<a name="line413"></a>goog.ui.ac.AutoComplete.prototype.hilitePrev = function() {
<a name="line414"></a>  if (this.hiliteId_ &gt; this.firstRowId_) {
<a name="line415"></a>    this.hiliteId(this.hiliteId_ - 1);
<a name="line416"></a>    return true;
<a name="line417"></a>  } else if (this.allowFreeSelect_ &amp;&amp; this.hiliteId_ == this.firstRowId_) {
<a name="line418"></a>    this.hiliteId(-1);
<a name="line419"></a>    return false;
<a name="line420"></a>  } else if (this.wrap_ &amp;&amp;
<a name="line421"></a>      (this.hiliteId_ == -1 || this.hiliteId_ == this.firstRowId_)) {
<a name="line422"></a>    var lastId = this.firstRowId_ + this.rows_.length - 1;
<a name="line423"></a>    this.hiliteId(lastId);
<a name="line424"></a>    return true;
<a name="line425"></a>  }
<a name="line426"></a>  return false;
<a name="line427"></a>};
<a name="line428"></a>
<a name="line429"></a>
<a name="line430"></a>/**
<a name="line431"></a> * Hilites the id if it&#39;s valid, otherwise does nothing.
<a name="line432"></a> * @param {number} id A row id (not index).
<a name="line433"></a> * @return {boolean} Whether the id was hilited.
<a name="line434"></a> */
<a name="line435"></a>goog.ui.ac.AutoComplete.prototype.hiliteId = function(id) {
<a name="line436"></a>  this.hiliteId_ = id;
<a name="line437"></a>  this.renderer_.hiliteId(id);
<a name="line438"></a>  return this.getIndexOfId(id) != -1;
<a name="line439"></a>};
<a name="line440"></a>
<a name="line441"></a>
<a name="line442"></a>/**
<a name="line443"></a> * Hilites the index, if it&#39;s valid, otherwise does nothing.
<a name="line444"></a> * @param {number} index The row&#39;s index.
<a name="line445"></a> * @return {boolean} Whether the index was hilited.
<a name="line446"></a> */
<a name="line447"></a>goog.ui.ac.AutoComplete.prototype.hiliteIndex = function(index) {
<a name="line448"></a>  return this.hiliteId(this.getIdOfIndex_(index));
<a name="line449"></a>};
<a name="line450"></a>
<a name="line451"></a>
<a name="line452"></a>/**
<a name="line453"></a> * If there are any current matches, this passes the hilited row data to
<a name="line454"></a> * &lt;code&gt;selectionHandler.selectRow()&lt;/code&gt;
<a name="line455"></a> * @return {boolean} Whether there are any current matches.
<a name="line456"></a> */
<a name="line457"></a>goog.ui.ac.AutoComplete.prototype.selectHilited = function() {
<a name="line458"></a>  var index = this.getIndexOfId(this.hiliteId_);
<a name="line459"></a>  if (index != -1) {
<a name="line460"></a>    var selectedRow = this.rows_[index];
<a name="line461"></a>    var suppressUpdate = this.selectionHandler_.selectRow(selectedRow);
<a name="line462"></a>    if (this.triggerSuggestionsOnUpdate_) {
<a name="line463"></a>      this.token_ = null;
<a name="line464"></a>      this.dismissOnDelay();
<a name="line465"></a>    } else {
<a name="line466"></a>      this.dismiss();
<a name="line467"></a>    }
<a name="line468"></a>    if (!suppressUpdate) {
<a name="line469"></a>      this.dispatchEvent({
<a name="line470"></a>        type: goog.ui.ac.AutoComplete.EventType.UPDATE,
<a name="line471"></a>        row: selectedRow
<a name="line472"></a>      });
<a name="line473"></a>      if (this.triggerSuggestionsOnUpdate_) {
<a name="line474"></a>        this.selectionHandler_.update(true);
<a name="line475"></a>      }
<a name="line476"></a>    }
<a name="line477"></a>    return true;
<a name="line478"></a>  } else {
<a name="line479"></a>    this.dismiss();
<a name="line480"></a>    this.dispatchEvent(
<a name="line481"></a>        {
<a name="line482"></a>          type: goog.ui.ac.AutoComplete.EventType.UPDATE,
<a name="line483"></a>          row: null
<a name="line484"></a>        });
<a name="line485"></a>    return false;
<a name="line486"></a>  }
<a name="line487"></a>};
<a name="line488"></a>
<a name="line489"></a>
<a name="line490"></a>/**
<a name="line491"></a> * Returns whether or not the autocomplete is open and has a highlighted row.
<a name="line492"></a> * @return {boolean} Whether an autocomplete row is highlighted.
<a name="line493"></a> */
<a name="line494"></a>goog.ui.ac.AutoComplete.prototype.hasHighlight = function() {
<a name="line495"></a>  return this.isOpen() &amp;&amp; this.getIndexOfId(this.hiliteId_) != -1;
<a name="line496"></a>};
<a name="line497"></a>
<a name="line498"></a>
<a name="line499"></a>/**
<a name="line500"></a> * Clears out the token, rows, and hilite, and calls
<a name="line501"></a> * &lt;code&gt;renderer.dismiss()&lt;/code&gt;
<a name="line502"></a> */
<a name="line503"></a>goog.ui.ac.AutoComplete.prototype.dismiss = function() {
<a name="line504"></a>  this.hiliteId_ = -1;
<a name="line505"></a>  this.token_ = null;
<a name="line506"></a>  this.firstRowId_ += this.rows_.length;
<a name="line507"></a>  this.rows_ = [];
<a name="line508"></a>  window.clearTimeout(this.dismissTimer_);
<a name="line509"></a>  this.dismissTimer_ = null;
<a name="line510"></a>  this.renderer_.dismiss();
<a name="line511"></a>  this.dispatchEvent(goog.ui.ac.AutoComplete.EventType.SUGGESTIONS_UPDATE);
<a name="line512"></a>  this.dispatchEvent(goog.ui.ac.AutoComplete.EventType.DISMISS);
<a name="line513"></a>};
<a name="line514"></a>
<a name="line515"></a>
<a name="line516"></a>/**
<a name="line517"></a> * Call a dismiss after a delay, if there&#39;s already a dismiss active, ignore.
<a name="line518"></a> */
<a name="line519"></a>goog.ui.ac.AutoComplete.prototype.dismissOnDelay = function() {
<a name="line520"></a>  if (!this.dismissTimer_) {
<a name="line521"></a>    this.dismissTimer_ = window.setTimeout(goog.bind(this.dismiss, this), 100);
<a name="line522"></a>  }
<a name="line523"></a>};
<a name="line524"></a>
<a name="line525"></a>
<a name="line526"></a>/**
<a name="line527"></a> * Cancels any delayed dismiss events immediately.
<a name="line528"></a> * @return {boolean} Whether a delayed dismiss was cancelled.
<a name="line529"></a> * @private
<a name="line530"></a> */
<a name="line531"></a>goog.ui.ac.AutoComplete.prototype.immediatelyCancelDelayedDismiss_ =
<a name="line532"></a>    function() {
<a name="line533"></a>  if (this.dismissTimer_) {
<a name="line534"></a>    window.clearTimeout(this.dismissTimer_);
<a name="line535"></a>    this.dismissTimer_ = null;
<a name="line536"></a>    return true;
<a name="line537"></a>  }
<a name="line538"></a>  return false;
<a name="line539"></a>};
<a name="line540"></a>
<a name="line541"></a>
<a name="line542"></a>/**
<a name="line543"></a> * Cancel the active delayed dismiss if there is one.
<a name="line544"></a> */
<a name="line545"></a>goog.ui.ac.AutoComplete.prototype.cancelDelayedDismiss = function() {
<a name="line546"></a>  // Under certain circumstances a cancel event occurs immediately prior to a
<a name="line547"></a>  // delayedDismiss event that it should be cancelling. To handle this situation
<a name="line548"></a>  // properly, a timer is used to stop that event.
<a name="line549"></a>  // Using only the timer creates undesirable behavior when the cancel occurs
<a name="line550"></a>  // less than 10ms before the delayed dismiss timout ends. If that happens the
<a name="line551"></a>  // clearTimeout() will occur too late and have no effect.
<a name="line552"></a>  if (!this.immediatelyCancelDelayedDismiss_()) {
<a name="line553"></a>    window.setTimeout(goog.bind(this.immediatelyCancelDelayedDismiss_, this),
<a name="line554"></a>        10);
<a name="line555"></a>  }
<a name="line556"></a>};
<a name="line557"></a>
<a name="line558"></a>
<a name="line559"></a>/** @override */
<a name="line560"></a>goog.ui.ac.AutoComplete.prototype.disposeInternal = function() {
<a name="line561"></a>  goog.ui.ac.AutoComplete.superClass_.disposeInternal.call(this);
<a name="line562"></a>  delete this.inputToAnchorMap_;
<a name="line563"></a>  this.renderer_.dispose();
<a name="line564"></a>  this.selectionHandler_.dispose();
<a name="line565"></a>  this.matcher_ = null;
<a name="line566"></a>};
<a name="line567"></a>
<a name="line568"></a>
<a name="line569"></a>/**
<a name="line570"></a> * Callback passed to Matcher when requesting matches for a token.
<a name="line571"></a> * This might be called synchronously, or asynchronously, or both, for
<a name="line572"></a> * any implementation of a Matcher.
<a name="line573"></a> * If the Matcher calls this back, with the same token this AutoComplete
<a name="line574"></a> * has set currently, then this will package the matching rows in object
<a name="line575"></a> * of the form
<a name="line576"></a> * &lt;pre&gt;
<a name="line577"></a> * {
<a name="line578"></a> *   id: an integer ID unique to this result set and AutoComplete instance,
<a name="line579"></a> *   data: the raw row data from Matcher
<a name="line580"></a> * }
<a name="line581"></a> * &lt;/pre&gt;
<a name="line582"></a> *
<a name="line583"></a> * @param {string} matchedToken Token that corresponds with the rows.
<a name="line584"></a> * @param {!Array} rows Set of data that match the given token.
<a name="line585"></a> * @param {(boolean|goog.ui.ac.RenderOptions)=} opt_options If true,
<a name="line586"></a> *     keeps the currently hilited (by index) element hilited. If false not.
<a name="line587"></a> *     Otherwise a RenderOptions object.
<a name="line588"></a> * @private
<a name="line589"></a> */
<a name="line590"></a>goog.ui.ac.AutoComplete.prototype.matchListener_ =
<a name="line591"></a>    function(matchedToken, rows, opt_options) {
<a name="line592"></a>  if (this.token_ != matchedToken) {
<a name="line593"></a>    // Matcher&#39;s response token doesn&#39;t match current token.
<a name="line594"></a>    // This is probably an async response that came in after
<a name="line595"></a>    // the token was changed, so don&#39;t do anything.
<a name="line596"></a>    return;
<a name="line597"></a>  }
<a name="line598"></a>
<a name="line599"></a>  this.renderRows(rows, opt_options);
<a name="line600"></a>};
<a name="line601"></a>
<a name="line602"></a>
<a name="line603"></a>/**
<a name="line604"></a> * Renders the rows and adds highlighting.
<a name="line605"></a> * @param {!Array} rows Set of data that match the given token.
<a name="line606"></a> * @param {(boolean|goog.ui.ac.RenderOptions)=} opt_options If true,
<a name="line607"></a> *     keeps the currently hilited (by index) element hilited. If false not.
<a name="line608"></a> *     Otherwise a RenderOptions object.
<a name="line609"></a> */
<a name="line610"></a>goog.ui.ac.AutoComplete.prototype.renderRows = function(rows, opt_options) {
<a name="line611"></a>  // The optional argument should be a RenderOptions object.  It can be a
<a name="line612"></a>  // boolean for backwards compatibility, defaulting to false.
<a name="line613"></a>  var optionsObj = goog.typeOf(opt_options) == &#39;object&#39; &amp;&amp; opt_options;
<a name="line614"></a>
<a name="line615"></a>  var preserveHilited =
<a name="line616"></a>      optionsObj ? optionsObj.getPreserveHilited() : opt_options;
<a name="line617"></a>  var indexToHilite = preserveHilited ? this.getIndexOfId(this.hiliteId_) : -1;
<a name="line618"></a>
<a name="line619"></a>  // Current token matches the matcher&#39;s response token.
<a name="line620"></a>  this.firstRowId_ += this.rows_.length;
<a name="line621"></a>  this.rows_ = rows;
<a name="line622"></a>  var rendRows = [];
<a name="line623"></a>  for (var i = 0; i &lt; rows.length; ++i) {
<a name="line624"></a>    rendRows.push({
<a name="line625"></a>      id: this.getIdOfIndex_(i),
<a name="line626"></a>      data: rows[i]
<a name="line627"></a>    });
<a name="line628"></a>  }
<a name="line629"></a>
<a name="line630"></a>  var anchor = null;
<a name="line631"></a>  if (this.target_) {
<a name="line632"></a>    anchor = this.inputToAnchorMap_[goog.getUid(this.target_)] || this.target_;
<a name="line633"></a>  }
<a name="line634"></a>  this.renderer_.setAnchorElement(anchor);
<a name="line635"></a>  this.renderer_.renderRows(rendRows, this.token_, this.target_);
<a name="line636"></a>
<a name="line637"></a>  var autoHilite = this.autoHilite_;
<a name="line638"></a>  if (optionsObj &amp;&amp; optionsObj.getAutoHilite() !== undefined) {
<a name="line639"></a>    autoHilite = optionsObj.getAutoHilite();
<a name="line640"></a>  }
<a name="line641"></a>  if ((autoHilite || indexToHilite &gt;= 0) &amp;&amp;
<a name="line642"></a>      rendRows.length != 0 &amp;&amp;
<a name="line643"></a>      this.token_) {
<a name="line644"></a>    var idToHilite = indexToHilite &gt;= 0 ?
<a name="line645"></a>        this.getIdOfIndex_(indexToHilite) : this.firstRowId_;
<a name="line646"></a>    this.hiliteId(idToHilite);
<a name="line647"></a>  } else {
<a name="line648"></a>    this.hiliteId_ = -1;
<a name="line649"></a>  }
<a name="line650"></a>  this.dispatchEvent(goog.ui.ac.AutoComplete.EventType.SUGGESTIONS_UPDATE);
<a name="line651"></a>};
<a name="line652"></a>
<a name="line653"></a>
<a name="line654"></a>/**
<a name="line655"></a> * Gets the index corresponding to a particular id.
<a name="line656"></a> * @param {number} id A unique id for the row.
<a name="line657"></a> * @return {number} A valid index into rows_, or -1 if the id is invalid.
<a name="line658"></a> * @protected
<a name="line659"></a> */
<a name="line660"></a>goog.ui.ac.AutoComplete.prototype.getIndexOfId = function(id) {
<a name="line661"></a>  var index = id - this.firstRowId_;
<a name="line662"></a>  if (index &lt; 0 || index &gt;= this.rows_.length) {
<a name="line663"></a>    return -1;
<a name="line664"></a>  }
<a name="line665"></a>  return index;
<a name="line666"></a>};
<a name="line667"></a>
<a name="line668"></a>
<a name="line669"></a>/**
<a name="line670"></a> * Gets the id corresponding to a particular index.  (Does no checking.)
<a name="line671"></a> * @param {number} index The index of a row in the result set.
<a name="line672"></a> * @return {number} The id that currently corresponds to that index.
<a name="line673"></a> * @private
<a name="line674"></a> */
<a name="line675"></a>goog.ui.ac.AutoComplete.prototype.getIdOfIndex_ = function(index) {
<a name="line676"></a>  return this.firstRowId_ + index;
<a name="line677"></a>};
<a name="line678"></a>
<a name="line679"></a>
<a name="line680"></a>/**
<a name="line681"></a> * Attach text areas or input boxes to the autocomplete by DOM reference.  After
<a name="line682"></a> * elements are attached to the autocomplete, when a user types they will see
<a name="line683"></a> * the autocomplete drop down.
<a name="line684"></a> * @param {...Element} var_args Variable args: Input or text area elements to
<a name="line685"></a> *     attach the autocomplete too.
<a name="line686"></a> */
<a name="line687"></a>goog.ui.ac.AutoComplete.prototype.attachInputs = function(var_args) {
<a name="line688"></a>  // Delegate to the input handler
<a name="line689"></a>  var inputHandler = /** @type {goog.ui.ac.InputHandler} */
<a name="line690"></a>      (this.selectionHandler_);
<a name="line691"></a>  inputHandler.attachInputs.apply(inputHandler, arguments);
<a name="line692"></a>};
<a name="line693"></a>
<a name="line694"></a>
<a name="line695"></a>/**
<a name="line696"></a> * Detach text areas or input boxes to the autocomplete by DOM reference.
<a name="line697"></a> * @param {...Element} var_args Variable args: Input or text area elements to
<a name="line698"></a> *     detach from the autocomplete.
<a name="line699"></a> */
<a name="line700"></a>goog.ui.ac.AutoComplete.prototype.detachInputs = function(var_args) {
<a name="line701"></a>  // Delegate to the input handler
<a name="line702"></a>  var inputHandler = /** @type {goog.ui.ac.InputHandler} */
<a name="line703"></a>      (this.selectionHandler_);
<a name="line704"></a>  inputHandler.detachInputs.apply(inputHandler, arguments);
<a name="line705"></a>
<a name="line706"></a>  // Remove mapping from input to anchor if one exists.
<a name="line707"></a>  goog.array.forEach(arguments, function(input) {
<a name="line708"></a>    goog.object.remove(this.inputToAnchorMap_, goog.getUid(input));
<a name="line709"></a>  }, this);
<a name="line710"></a>};
<a name="line711"></a>
<a name="line712"></a>
<a name="line713"></a>/**
<a name="line714"></a> * Attaches the autocompleter to a text area or text input element
<a name="line715"></a> * with an anchor element. The anchor element is the element the
<a name="line716"></a> * autocomplete box will be positioned against.
<a name="line717"></a> * @param {Element} inputElement The input element. May be &#39;textarea&#39;,
<a name="line718"></a> *     text &#39;input&#39; element, or any other element that exposes similar
<a name="line719"></a> *     interface.
<a name="line720"></a> * @param {Element} anchorElement The anchor element.
<a name="line721"></a> */
<a name="line722"></a>goog.ui.ac.AutoComplete.prototype.attachInputWithAnchor = function(
<a name="line723"></a>    inputElement, anchorElement) {
<a name="line724"></a>  this.inputToAnchorMap_[goog.getUid(inputElement)] = anchorElement;
<a name="line725"></a>  this.attachInputs(inputElement);
<a name="line726"></a>};
<a name="line727"></a>
<a name="line728"></a>
<a name="line729"></a>/**
<a name="line730"></a> * Forces an update of the display.
<a name="line731"></a> * @param {boolean=} opt_force Whether to force an update.
<a name="line732"></a> */
<a name="line733"></a>goog.ui.ac.AutoComplete.prototype.update = function(opt_force) {
<a name="line734"></a>  var inputHandler = /** @type {goog.ui.ac.InputHandler} */
<a name="line735"></a>      (this.selectionHandler_);
<a name="line736"></a>  inputHandler.update(opt_force);
<a name="line737"></a>};
</pre>


</body>
</html>
